//## 원자재 정보  정보 저장 ####################
function fnSave()
{
	if (!$("#srcCd").val())
	{
		alert("원재자를 바르게 선택해 주세요!");
		$("#srcCd").focus();
		return;
	}
	if (!$("#input").val() && !$("#output").val())
	{
		alert("입/출고 수량을 입력해 주세요.");
		$("#input").focus();
		return;
	}
	var valUrl  = '/action/srcStore/save.asp';
	var param  = "srcCd="+$("#srcCd").val();
		param += "&input="+$("#input").val();
		param += "&output="+$("#output").val();
		param += "&issuedMemo="+encodeURIComponent($("#issuedMemo").val());
		param += "&issuedDt="+$("#issuedDt").val();
		param += "&seq="+$("#seq").val();
		param += "&flag="+$("#flag").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 원자재 정보  정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	var valUrl = "/action/srcStore/list.asp";
	var param = "findSrcCd="+$("#findSrcCd").val();
		param += "&findIssuedMemo="+encodeURIComponent($("#findIssuedMemo").val());
		param += "&stDate="+$("#stDate").val();
		param += "&endDate="+$("#endDate").val();
		if ($("#findFlag").is(":checked")) param += "&findFlag=Y";

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	var cellWidth = new Array();
    var cellClass = new Array();
	for (var c=0; c<$("#Grid01 td").length; c++)
	{
		cellWidth[c] = $("#Grid01 td").eq(c).attr("width");
		cellClass[c] = $("#Grid01 td").eq(c).attr("class").replace(" gridTitle","");
	}
	var nodeCnt = $(G_grid01Rslt).find("Sources").find("SrcList").length;

	if(nodeCnt == 0)
	{
		var rsltMsg = "<tr bgcolor=\"#FFFFFF\" height=\"30\" align=\"center\">";
		rsltMsg += "		<td colspan=\"6\" style=\"color:#948D88; BORDER-TOP: #abccf8 0px solid;height:35px\" class=\"lTD_0rbl\">";
		rsltMsg += "		검색된 자료가 없습니다.";
		rsltMsg += "		</td>";
		rsltMsg += "</tr>";
		
		$("#tr01Result").html();
		$("#grid01Cnt").html("0");
		$("#pageDiv01").html("");
		return;
	}
	var listNum = 10;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	$("#tr01Result").html("");
	for(var j=startNo; j<endNo; j++)
	{
		var seq					= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("seq").text();
		var srcNm				= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("srcNm").text();
		var input				= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("input").text();
		var output				= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("output").text();
		var issuedMemo	= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("issuedMemo").text();
		var issuedDt			= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("issuedDt").text();
		var flag					= $(G_grid01Rslt).find("Sources").find("SrcList").eq(j).find("flag").text();

		ji = j+1;
		
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "유효";
		else showFlag = "삭제";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else var bgColor = "#FFFFFF";

		var curRows = "<tr style=\"cursor:pointer;background-color:"+bgColor+"\" onClick=\"fnSetSrcStore('"+seq+"')\">";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[0]+"\" class=\""+cellClass[0]+"\">"+ji+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[1]+"\" class=\""+cellClass[1]+"\">"+tagFirst + srcNm + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[2]+"\" class=\""+cellClass[2]+"\">"+tagFirst + input + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[3]+"\" class=\""+cellClass[3]+"\">"+tagFirst + output + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[4]+"\" class=\""+cellClass[4]+"\">"+tagFirst + issuedDt + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:left;\" width=\""+cellWidth[5]+"\" class=\""+cellClass[5]+"\">"+tagFirst + issuedMemo +"&nbsp;" + tagLast+"</td>";
		curRows += "<td style=\"height:35px;text-align:center;\" width=\""+cellWidth[6]+"\" class=\""+cellClass[6]+"\">"+showFlag+"</td>";
		curRows += "</tr>";

		$("#tr01Result").append(curRows);
	}
}


//## 원자재 상세정보 표시 ####################
function fnSetSrcStore(seq)
{
	var valUrl = "/action/srcStore/view.asp";
	var param = "seq="+seq;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();			
			$("#srcCd").val($(xml).find("srcCd").text());
			$("#input").val($(xml).find("input").text());
			$("#output").val($(xml).find("output").text());
			$("#issuedMemo").val($(xml).find("issuedMemo").text());
			$("#seq").val($(xml).find("seq").text());
			$("#issuedDt").val($(xml).find("issuedDt").text());
			var accInfo = "최종수정 : "+ $(xml).find("actionDt").text() + " / " + $(xml).find("userNm").text();
			$("#accessInfo").val(accInfo);
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 원자재 정보  정보 Reset 기능
function fnReset()
{
	$("#srcCd").val("");
	$("#input").val("");
	$("#output").val("");
	$("#issuedMemo").val("");
	$("#seq").val("");
	$("#accessInfo").val("");
	$("#issuedDt").val("");
	$("#flag").val("Y");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findSrcCd").val("");
	$("#findIssuedMemo").val("");
}